# BSD3.0
# cili@163.com

from pathlib import Path
import re
import csv

def read_csv(filename):
    column1 = []
    column2 = []
    if not Path(filename).exists(): return column1, column2
    for encode in ['utf-8-sig','gbk']:
        try:
            with open(filename, 'r', newline='', encoding=encode) as file:
                reader = csv.reader(file)
                for row in reader:
                    if len(row) >= 2:  # 确保每行至少有两列
                        column1.append(row[0])
                        column2.append(row[1])
        except:
            continue
    return column1, column2

# 获取网页标题
def get_title(html_content):
    title_pattern = re.compile(r'<title>(.*?)</title>')
    title_match = title_pattern.search(html_content)
    try:
        return title_match.group(1)
    except:
        return None


def search_keyword(keyword):
    result = []
    root_path = Path('web')

    # 遍历目录中的所有文件
    for file_path in root_path.iterdir():
        if file_path.suffix != '.csv' and not file_path.is_dir():
            with file_path.open('r', encoding='utf-8') as file:
                content = file.read()
                text = re.sub(r'<[^>]+>', '', content)
                n = text.lower().count(keyword.lower())  # 忽略大小写进行关键字匹配
                if n > 0:
                    title = get_title(content)
                    filename_without_extension = file_path.stem
                    try:
                        index = pages_name.index(filename_without_extension)
                        result.append((n, pages_url[index], title))
                    except ValueError:
                        pass
    return result

pages_name ,pages_url = read_csv('BAK/web/pages.csv')

